// @ 516
// _(:з」∠)_

define([
    'leafletCustom',
    'leafletMultiFacet',
    'leafletGridHeatMap'
], function() {
    'use strict';

    // bind map tool event
    MC.bindMapToolsEvent = function( bindParam ) {
        if (
            typeof bindParam == "object"
            && bindParam.length != undefined
        ) {
            for ( let x = 0, len = bindParam.length; x < len; x++ ) {
                bindParam[x][0].addEventListener( "click", function(e) {
                    MC.mapEvent.removeAllEvent( false );

                    if ( bindParam[x][0].hasClass('select') ) {
                        bindParam[x][0].removeClass('select');

                        switch( bindParam[x][1] ) {
                            case "任意点": MC.mapEvent.drawPoint( false, bindParam[x][0] ); break;
                            case "灾害点": MC.mapEvent.drawDisPoint( false, bindParam[x][0] ); break;
                            case "行政区":  MC.mapEvent.drawDistrict( false, bindParam[x][0] ); break;
                            case "区域圈选":  MC.mapEvent.drawPolygon( false, bindParam[x][0] ); break;
                            case "切换底图":  MC.mapEvent.changeMapBox( false, bindParam[x][0] ); break;
                        }
                    } else {
                        bindParam[x][0].removeAllClass('select');
                        bindParam[x][0].addClass('select');

                        switch( bindParam[x][1] ) {
                            case "任意点": MC.mapEvent.drawPoint( true, bindParam[x][0] ); break;
                            case "灾害点": MC.mapEvent.drawDisPoint( true, bindParam[x][0] ); break;
                            case "行政区":  MC.mapEvent.drawDistrict( true, bindParam[x][0] ); break;
                            case "区域圈选":  MC.mapEvent.drawPolygon( true, bindParam[x][0] ); break;
                            case "切换底图":  MC.mapEvent.changeMapBox( false, bindParam[x][0] ); break;
                        }
                    }
                }, false);
            }
        }
    }

    // add map draw bounds event
    MC.mapEvent["drawBounds"] = function( filePath, param, type ) {
        RST.areaBounds( filePath, param, type, function( data ) {
            if ( MC.mapLayer.boundLayer ) {
                MC.map.removeLayer( MC.mapLayer.boundLayer );
            }
            MC.mapLayer.boundLayer = null;
            MC.mapLayer.boundLayer = L.Proj.geoJson( data, {
                style: {
                    fill: false,
                    color: "#1bd01b",
                    weight: 1,
                    stroke: true
                }
            });
            MC.mapLayer.boundLayer.addTo( MC.map );
    
            MC.mapEvent.setCenter( MC.mapEvent.getBounds( MC.mapLayer.boundLayer ) );
        });
    }

    // get layer bounds
    MC.mapEvent["getBounds"] = function( layer ) {
        let bounds = [ [], [] ],
            layerBounds = layer.getBounds();
        
        bounds[0][0] = layerBounds._northEast.lat;
        bounds[0][1] = layerBounds._northEast.lng;
        bounds[1][0] = layerBounds._southWest.lat;
        bounds[1][1] = layerBounds._southWest.lng;

        return bounds;
    }

    // add map draw chromatic event
    MC.mapEvent["drawChromatic"] = function( result, option ) {
        if ( !result.detail ) {
            Popup.tipPopup({ type: "toast", content: "当前无数据" });
            return false;
        }
        let vtJson = result.detail.replace(/<\/?.+?>/g,"").replace(/[\r\n]/g, ""),
            data = JSON.parse( vtJson ),
            colorArry = data[1].items,
            ctColor = [];
        
        Title.topTitle({
            main: data[0].Metadata.MainTitle,
            sub: data[0].Metadata.SubTitle
        });

        data[2]["crs"] = { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } };

        if ( MC.mapLayer.chromaticLayer ) {
            MC.map.removeLayer( MC.mapLayer.chromaticLayer );
        }
        MC.mapLayer.chromaticLayer = null;
        MC.mapLayer.chromaticLayer = L.Proj.geoJson( data[2], {
            style: function( feature ) {
                let MGMinValue = feature.properties.MGMinValue,
                    MGMaxValue = feature.properties.MGMaxValue;
                if ( MGMinValue <= -90 ) { MGMinValue = MGMaxValue; }

                for ( let x = colorArry.length - 1; x >= 0; x-- ) {
                    if ( MGMinValue >= colorArry[x].Value ) {
                        ctColor.push( colorArry[x] );
                        return {
                            color: colorArry[x].FillStyle.ForeColor,
                            weight: 1,
                            fillColor: colorArry[x].FillStyle.ForeColor,
                            fillOpacity: option.opacity
                        };
                    }
                }
            }
        });
        
        ctColor = objToHeavy( ctColor, "Caption" );
        ctColor = ctColor.sort( objCompare( "Caption" ) );
        Legend.level( ctColor );

        MC.mapLayer.chromaticLayer.addTo( MC.map );
        MC.mapAllLayer = [];

        for ( let x in MC.map._layers ) {
            MC.mapAllLayer.push( MC.map._layers[x] );
        }
    }

    // add map draw grid event
    MC.mapEvent["drawGrid"] = function( filePath, param ) {
        
    }

    // add map image layer
    MC.mapEvent["drawImageLayer"] = function( reuslt, option ) {
        let url = NetWorkPath + "/" + reuslt.relativePath;

        if ( option.title ) {
            Title.topTitle({
                main: reuslt.productMaintitle,
                sub: reuslt.productSubtitle
            });
        }

        if ( MC.mapLayer.imageLayer ) {
            MC.map.removeLayer( MC.mapLayer.imageLayer );
        }

        MC.mapLayer.imageLayer = null;

        MC.mapLayer.imageLayer = L.imageOverlay(
            url,
            [
                [ 28.790434611931115, 12.378784615167007 ],
                [ 39.58626269400332, 22.314151657528914 ]
            ],
            { opacity: option.opacity }
        );

        MC.mapLayer.imageLayer.addTo( MC.map );
    }

    // map set center
    MC.mapEvent["setCenter"] = function( bounds, zoom ) {
        if ( bounds.length <= 1 && zoom ) {
            MC.map.setView( bounds[0], zoom );
        } else {
            MC.map.fitBounds( bounds );
        }
    }

    // add map draw point event
    MC.mapEvent["drawPoint"] = function( status, elmt ) {
        if ( MC.mapToolEvent ) {
            if ( MC.mapLayer.chromaticLayer
                || MC.mapLayer.gridLayer
                || MC.mapLayer.imageLayer
                || gridHeatMap.isItExists( MC.map._layers )
            ) {
                if ( status ) {
                    MC.map.on( "click", function(e) {
                        let latlng = e.latlng,
                            myIcon = L.icon({
                                iconUrl: Path + "/image/icon/point_red.png",
                                iconSize: [ 18, 24 ],
                                iconAnchor: [ 9, 20 ],
                                popupAnchor: [ 0, -22 ]
                            });

                        MC.mapEvent.clearAllCover( MC.mapMarker );

                        // Albers to latLng
                        let _latLng = MC.customCRS.project( L.latLng([ latlng.lat, latlng.lng ]) ),
                            _latLng_ = proj4( MC.China_Albers ).inverse([ _latLng.x, _latLng.y ]);

                        RST.lngLatTurnAddress( { lat: _latLng_[1], lng: _latLng_[0] }, function( result ) {
                            let html = ``,
                                address = result.regeocode.addressComponent,
                                marker = L.marker([ latlng.lat, latlng.lng ], {
                                    icon: myIcon, alt: "受灾点"
                                }).addTo( MC.map ).bindPopup( address.province+address.city+address.district ).openPopup();

                            MC.mapMarker.push( marker );

                            let box = document.querySelector(".disaster_box"),
                                item = box.querySelector(".ul_list > .li_item.select"),
                                itemx = box.querySelector(".item_child_list .li_item.select");

                            rightPanel.init({
                                type: "point",
                                panelType: "disaster",
                                disType: item.getAttribute("distype"),
                                disArea: address.adcode,
                                disModel: itemx.getAttribute("type"),
                                latlng: [ _latLng.y, _latLng.x ]
                            });
 
                            marker.on('click', function(e) {
                                let rightMain = document.querySelector(".rightMain");
                                if ( rightMain ) {
                                    rightMain.remove();
                                } else {            
                                    rightPanel.init({
                                        type: "point",
                                        panelType: "disaster",
                                        disType: item.getAttribute("distype"),
                                        disArea: item.getAttribute("areaval"),
                                        disModel: itemx.getAttribute("type"),
                                        latlng: [ _latLng.y, _latLng.x ]
                                    });
                                }
                            });

                            marker.on("contextmenu", function( e ) {
                                let evt = e,
                                    markerMenu = document.querySelector(".markerMenu");

                                if ( markerMenu ) markerMenu.remove();
                
                                let menu = `<ul class="menu_ul">
                                                <li class="menu_li" type="remove">灾害点移除</li>
                                                <li class="menu_li" type="save">灾害点保存</li>
                                            </ul>`,
                                    popup =  L.popup({
                                        className: "markerMenu",
                                        closeButton: false
                                    }).setLatLng([
                                        evt.latlng.lat, evt.latlng.lng
                                    ]).setContent( menu ).addTo( MC.map );
                
                                MC.mapPopup.push( popup );
                
                                markerMenu = document.querySelector(".markerMenu");
                                markerMenu.addEventListener("click", function( e ) {
                                    if ( e.target.nodeName == "LI" ) {
                                        markerMenu.remove();

                                        let userInfo = localStorage.userInfo,
                                            userSavePoint = null, pointNum = null,
                                            rightPanel = document.querySelector(".rightPanel");

                                        if ( userInfo && userInfo != "" ) {
                                            userInfo = JSON.parse( userInfo );
                                            userSavePoint = localStorage[ userInfo.name ];

                                            if ( userSavePoint && userSavePoint != "" ) {
                                                userSavePoint = JSON.parse( userSavePoint );
                                            } else { userSavePoint = [] }
                                        } else {
                                            Popup.tipPopup({ type: "toast", content: "未获取到用户信息" });
                                            return false;
                                        }

                                        switch( e.target.getAttribute("type") ) {
                                            case "remove":
                                                if ( rightPanel ) rightPanel.remove();

                                                MC.map.removeLayer( marker );
                                                MC.mapEvent.clearAllCover();
                                            break;
                                            case "save":
                                                if ( userSavePoint.length > 0 ) {
                                                    for ( let x = 0; x < userSavePoint.length; x++ ) {
                                                        if ( userSavePoint[x].name == evt.target._popup._content ) {
                                                            userSavePoint.splice( x, 1 );
                                                            break;
                                                        }
                                                    }
                                                }
                                                
                                                if ( userSavePoint.length >= 10 ) {
                                                    Popup.tipPopup({ type: "toast", content: "已保存的点数量已到上限( 10个 )" });
                                                    return false;
                                                }

                                                userSavePoint.push({
                                                    name: evt.target._popup._content,
                                                    lat: evt.latlng.lat,
                                                    lng: evt.latlng.lng
                                                });

                                                localStorage[ userInfo.name ] = JSON.stringify( userSavePoint );
                                            break;
                                        }
                                    }

                                    return false;
                                }, false);
                            });
                
                            marker.on("remove", function( e ) {
                                console.log("remove marker");
                            });
                        });
                    });
                } else {
                    let rightPopup = document.querySelector(".rightPopup");
                    if ( rightPopup ) rightPopup.remove();
                    MC.mapEvent.removeAllEvent( false );
                }
            } else {
                Popup.tipPopup({ type: "toast", content: "请先向地图中叠加数据层" });
                elmt.removeClass("select");
            }
        } else {
            Popup.tipPopup({ type: "toast", content: "请选择灾情信息模块后使用" });
            elmt.removeClass("select");
        }
    }

    MC.mapEvent["drawDisPoint"] = function( status, elmt ) {
        if ( MC.mapToolEvent ) {
            if ( MC.mapLayer.chromaticLayer
                || MC.mapLayer.gridLayer
                || MC.mapLayer.imageLayer
                || gridHeatMap.isItExists( MC.map._layers )
            ) {
                if ( status ) {
                    let box = document.querySelector(".disaster_box"),
                        item = box.querySelector(".ul_list > .li_item.select"),
                        itemx = box.querySelector(".item_child_list .li_item.select");

                    let marker = null, userSavePoint = null,
                        myIcon = L.icon({
                            iconUrl: Path + "/image/icon/point_red.png",
                            iconSize: [ 18, 24 ],
                            iconAnchor: [ 9, 20 ],
                            popupAnchor: [ 0, -22 ]
                        }),
                        userInfo = localStorage.userInfo;
                        
                    if ( userInfo && userInfo != "" ) {
                        userInfo = JSON.parse( userInfo );
                        userSavePoint = localStorage[ userInfo.name ];

                        if ( userSavePoint && userSavePoint != "" ) {
                            userSavePoint = JSON.parse( userSavePoint );

                            for ( let x = 0; x < userSavePoint.length; x++ ) {
                                marker = L.marker([
                                    Number( userSavePoint[x].lat ),
                                    Number( userSavePoint[x].lng )
                                ], {
                                    icon: myIcon, alt: "受灾点"
                                }).addTo( MC.map ).bindPopup( userSavePoint[x].name ).openPopup();
    
                                MC.mapMarker.push( marker );
                                
                                marker.on('click', function(e) {
                                    let rightMain = document.querySelector(".rightMain");
                                    if ( rightMain ) {
                                        rightMain.remove();
                                    } else {            
                                        rightPanel.init({
                                            type: "point",
                                            panelType: "disaster",
                                            disType: item.getAttribute("distype"),
                                            disArea: item.getAttribute("areaval"),
                                            disModel: itemx.getAttribute("type"),
                                            latlng: [ e.latlng.lat, e.latlng.lng ]
                                        });
                                    }
                                });
            
                                marker.on("contextmenu", function( e ) {
                                    let evt = e,
                                        markerMenu = document.querySelector(".markerMenu");
            
                                    if ( markerMenu ) markerMenu.remove();
                    
                                    let menu = `<ul class="menu_ul">
                                                    <li class="menu_li" type="remove">灾害点移除</li>
                                                </ul>`,
                                        popup =  L.popup({
                                            className: "markerMenu",
                                            closeButton: false
                                        }).setLatLng([
                                            evt.latlng.lat, evt.latlng.lng
                                        ]).setContent( menu ).addTo( MC.map );
                    
                                    MC.mapPopup.push( popup );
                    
                                    markerMenu = document.querySelector(".markerMenu");
                                    markerMenu.addEventListener("click", function( e ) {
                                        if ( e.target.nodeName == "LI" ) {
                                            markerMenu.remove();
                                            userSavePoint = localStorage[ userInfo.name ];
            
                                            let rightPanel = document.querySelector(".rightPanel");
            
                                            switch( e.target.getAttribute("type") ) {
                                                case "remove":
                                                    if ( rightPanel ) rightPanel.remove();
            
                                                    MC.map.removeLayer( marker );
                                                    MC.mapEvent.clearAllCover();

                                                    if ( userSavePoint.length > 0 ) {
                                                        for ( let x = 0; x < userSavePoint.length; x++ ) {
                                                            if ( userSavePoint[x].name == evt.target._popup._content ) {
                                                                userSavePoint.splice( x, 1 );
                                                                break;
                                                            }
                                                        }
                                                        localStorage[ userInfo.name ] = JSON.stringify( userSavePoint );
                                                    }
                                                break;
                                            }
                                        }
            
                                        return false;
                                    }, false);
                                });
                    
                                marker.on("remove", function( e ) {
                                    console.log("remove marker");
                                });
                            }
                        } else {
                            Popup.tipPopup({ type: "toast", content: "当前用户没有灾害点" });
                            return false;
                        }
                    } else {
                        Popup.tipPopup({ type: "toast", content: "未获取到用户信息" });
                        return false;
                    }
                } else {
                    let rightPopup = document.querySelector(".rightPopup");
                    if ( rightPopup ) rightPopup.remove();
                    MC.mapEvent.removeAllEvent( false );
                }
            } else {
                Popup.tipPopup({ type: "toast", content: "请先向地图中叠加数据层" });
                elmt.removeClass("select");
            }
        } else {
            Popup.tipPopup({ type: "toast", content: "请选择灾情信息模块后使用" });
            elmt.removeClass("select");
        }
    }

    // 小气候站
    MC.mapEvent["drawFarmlandStations"] = function( result, execute ) {
        MC.mapEvent.clearAllCover( MC.mapStation );

        for ( let x = 0; x < result.length; x++ ) {
            let myIcon = L.icon({
                    iconUrl: Path + "/image/icon/TQ.png",
                    iconSize: [ 30, 33 ],
                    iconAnchor: [ 15, 28 ],
                    popupAnchor: [ 0, -30 ]
                }),
                marker = L.marker([ result[x].lat, result[x].lng ], {
                    icon: myIcon,
                    index: x,
                    detail: result[x],
                    alt: "小气候站"
                }).addTo( MC.map ).bindPopup( result[x].name );

            MC.mapStation.push( marker );

            marker.on('click', function(e) {
                ( execute && typeof( execute ) === "function" ) && execute( e );
            });
        }
    }

    // 灾害上报
    MC.mapEvent["disSituation"] = function( result, execute ) {
        MC.mapEvent.clearAllCover( MC.mapStation );

        for ( let x = 0; x < result.length; x++ ) {
            let myIcon = L.icon({
                    iconUrl: Path + "/image/image/dis_ico.png",
                    iconSize: [ 30, 33 ],
                    iconAnchor: [ 15, 28 ],
                    popupAnchor: [ 0, -30 ]
                }),
                marker = L.marker([ result[x].lat, result[x].lng ], {
                    icon: myIcon,
                    index: x,
                    detail: result[x],
                    alt: "灾害点"
                }).addTo( MC.map ).bindPopup( result[x].name );

            MC.mapStation.push( marker );

            marker.on('click', function(e) {
                ( execute && typeof( execute ) === "function" ) && execute( e );
            });
        }
    }

    // add map draw district event
    MC.mapEvent["drawDistrict"] = function( status ) {
        if ( MC.mapLayer.chromaticLayer
            || MC.mapLayer.gridLayer )
        {
            if ( status ) {
                MC.map.on( "click", function(e) {
                    let latlng = e.latlng;

                    MC.mapEvent.clearAllCover();

                    RST.lngLatTurnAddress( latlng, function( result ) {
                        let address = result.regeocode.addressComponent,
                            html = `<h class="optTip">请选择一个行政区</h>
                                        <ul class="areaOpt">
                                            <li class="areaOpt_item">${ address.province }</li>
                                            <li class="areaOpt_item">${ address.city }</li>
                                            <li class="areaOpt_item">${ address.district }</li>
                                        </ul>`,
                            popup =  L.popup().setLatLng([
                                            latlng.lat, latlng.lng
                                        ]).setContent( html ).addTo( MC.map );

                        MC.mapPopup.push( popup );

                        // 确认行政区并获取边界
                        let areaOpt = document.querySelector("ul.areaOpt");
                        if ( areaOpt ) {
                            let areaItem = areaOpt.querySelectorAll("li.areaOpt_item");
                            for ( let x = 0; x < areaItem.length; x++ ) {
                                areaItem[x].addEventListener( "click", function( e ) {
                                    MC.mapEvent.clearAllCover();
                                    MC.mapEvent.drawBounds( "",  areaItem[x].textContent, "tianditu" );

                                    require([ "rightMain" ]);
                                }, false);
                            }
                        }
                    });
                });
            } else {
                MC.mapEvent.removeAllEvent( false );
            }
        } else {
            console.log( "_请先向地图中添加相应图层后操作" );
        }
    }

    // add map draw polygon event
    MC.mapEvent["drawPolygon"] = function( status ) {
        if ( status ) {
            let points = [],
                lines = new L.polyline([], { color: '#1bd01b', weight: 1 }),
                tempLines = new L.polygon([], { color: '#1bd01b', weight: 1 });

            MC.map.on('click', onClick );
            MC.map.on('dblclick', onletClick );
            MC.map.on('mousemove', onMove );

            function onClick(e) {
                let circle = L.circle( e.latlng, {
                    color: '#1bd01b',
                    weight: 2,
                    fillColor: '#1bd01b',
                    fillOpacity: 1
                });
                points.push([ e.latlng.lat, e.latlng.lng ]);
                lines.addLatLng( e.latlng );
                MC.map.addLayer( lines );
                MC.map.addLayer( circle );

                MC.mapCircle.push( circle );
                MC.mapPolyline.push( lines );
            }

            function onMove(e) {
                if  ( points.length > 0 ) {
                    let ls = [ points[ points.length - 1 ], [ e.latlng.lat, e.latlng.lng ] ];
                    tempLines.setLatLngs( ls );
                    MC.map.addLayer( tempLines );

                    MC.mapPolyline.push( tempLines );
                }
            }

            function onletClick(e) {
                let polygon = L.polygon([ points ], {
                    color: '#1bd01b',
                    weight: 1,
                    fillColor: '#1bd01b',
                    fillOpacity: .1
                });
                polygon.addTo( MC.map );
                lines = new L.polyline([], { color: '#1bd01b', weight: 1 });
                points = [];
                
                MC.mapPolygon.push( polygon );
            }
        } else {
            MC.mapEvent.removeAllEvent( false );
        }
    }

    // change map
    MC.mapEvent["changeMapBox"] = function() {
        if ( status ) {

            
        } else {
            MC.mapEvent.removeAllEvent( false );
        }
    }


    // ------------------------------------------------------------------------------------
    // event tools

    // lngLatToAlbers
    MC.mapEvent["lngLatToAlbers"] = function( lngLat ) {
        
    }

    // AlbersTolngLat
    MC.mapEvent["AlbersTolngLat"] = function( CRS, Albers ) {
        let China_Albers = "+proj=aea +lat_0=0 +lat_1=25 +lat_2=47 +lon_0=95 +x_0=0 +y_0=0 +k=1 +ellps=WGS84 +datum=WGS84 +units=m +no_defs",
            Albers_ = proj4( China_Albers  ).forward([ Albers.lng, Albers.lat ]),
            latLng = CRS.unproject( L.point( Albers_[0], Albers_[1] ) );
        return { lat: latLng.lat, lng: latLng.lng, albersLat: Albers_[1], albersLng: Albers_[0] }
    }

    // remove map all event
    MC.mapEvent["removeAllEvent"] = function() {
        // close map event
        if ( MC.map.listens( 'click' ) ) MC.map.off( "click" );
        if ( MC.map.listens( 'dblclick' ) ) MC.map.off( "dblclick" );
        if ( MC.map.listens( 'mousemove' ) ) MC.map.off( "mousemove" );

        // MC.mapEvent.clearAllCover();
    }

    // clear map all cover
    MC.mapEvent["clearAllCover"] = function( appoint ) {
        if ( appoint ) {
            if ( appoint.length > 0 ) {
                for ( let x = 0; x < appoint.length; x++ ) {
                    MC.map.removeLayer( appoint[x] );
                }
                appoint = [];
            }
        } else {
            // close draw point tool and clear map marker
            if ( MC.mapMarker.length > 0 ) {
                for ( let x = 0; x < MC.mapMarker.length; x++ ) {
                    MC.map.removeLayer( MC.mapMarker[x] );
                }
                MC.mapMarker = [];
            }

            // close draw circle tool and clear map popup
            if ( MC.mapCircle.length > 0 ) {
                for ( let x = 0; x < MC.mapCircle.length; x++ ) {
                    MC.map.removeLayer( MC.mapCircle[x] );
                }
                MC.mapCircle = [];
            }

            // close draw polyline tool and clear map popup
            if ( MC.mapPolyline.length > 0 ) {
                for ( let x = 0; x < MC.mapPolyline.length; x++ ) {
                    MC.map.removeLayer( MC.mapPolyline[x] );
                }
                MC.mapPolyline = [];
            }

            // close draw polygon tool and clear map popup
            if ( MC.mapPolygon.length > 0 ) {
                for ( let x = 0; x < MC.mapPolygon.length; x++ ) {
                    MC.map.removeLayer( MC.mapPolygon[x] );
                }
                MC.mapPolygon = [];
            }

            // close draw district tool and clear map popup
            if ( MC.mapPopup.length > 0 ) {
                for ( let x = 0; x < MC.mapPopup.length; x++ ) {
                    MC.map.removeLayer( MC.mapPopup[x] );
                }
                MC.mapPopup = [];
            }

            // close draw point tool and clear map marker
            if ( MC.mapStation.length > 0 ) {
                for ( let x = 0; x < MC.mapStation.length; x++ ) {
                    MC.map.removeLayer( MC.mapStation[x] );
                }
                MC.mapStation = [];
            }
        }
    }

    // close map all event and chromatic and grid and query bounds
    MC.mapEvent["closeEventAndLayer"] = function() {
        MC.mapEvent.removeAllEvent( true );
        document.querySelector(".tools_item").removeAllClass("select");

        if ( MC.mapLayer.chromaticLayer ) {
            MC.map.removeLayer( MC.mapLayer.chromaticLayer );
            MC.mapLayer.chromaticLayer = null;
        }

        if ( MC.mapLayer.imageLayer ) {
            MC.map.removeLayer( MC.mapLayer.imageLayer );
            MC.mapLayer.imageLayer = null;
        }

        if ( MC.mapLayer.gridLayer ) {
            MC.map.removeLayer( MC.mapLayer.gridLayer );
            MC.mapLayer.gridLayer = null;
        }

        gridHeatMap.removeGrid( MC.map._layers );

        // MC.mapEvent.getCurrentAreaBounds();
    }

    MC.mapEvent["closeAllLayer"] = function() {
        if ( MC.mapLayer.chromaticLayer ) {
            MC.map.removeLayer( MC.mapLayer.chromaticLayer );
            MC.mapLayer.chromaticLayer = null;
        }

        if ( MC.mapLayer.imageLayer ) {
            MC.map.removeLayer( MC.mapLayer.imageLayer );
            MC.mapLayer.imageLayer = null;
        }

        if ( MC.mapLayer.gridLayer ) {
            MC.map.removeLayer( MC.mapLayer.gridLayer );
            MC.mapLayer.gridLayer = null;
        }

        gridHeatMap.removeGrid( MC.map._layers );
    }

    // get current area bounds
    MC.mapEvent["getCurrentAreaBounds"] = function() {
        let areaName = "",
            currentArea = document.querySelector(".change_area > h"),
            currentAreax = document.querySelector(".disasters_item.select > div");

        if ( currentAreax ) {
            areaName = currentAreax.getAttribute("areaname");
        } else if ( currentArea ) {
            areaName = currentArea.getAttribute("name");
        }

        MC.mapEvent.drawBounds( "/data/bounds/"+ areaName +".json", "", "" );
    }
});